package com.woniuxy.user.controller;

import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.entity.User;
import com.woniuxy.commons.entity.UserLike;
import com.woniuxy.user.service.UserLikeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author:阳佳
 * @Date: 2020/7/20 19:24
 * @Description:
 * @PACKAGE_NAME:com.woniuxy.user.controller
 */
@Api(tags = "点赞相关操作")
@RestController
@RequestMapping("/user")
public class UserLikeController {
    @Autowired
    private UserLikeService userLikeService;

    /*
    对某个课程点赞
     */
    @ApiOperation("增加一条点赞记录")
    @PostMapping ("like/{cid}")
    public ResponseEntity like(@ApiParam("课程id") @PathVariable String cid, HttpSession httpSession) {
        //进入某个课程后 可以对某个课程进行点赞
        User user = (User) httpSession.getAttribute("user");
        //获取用户id
        String uid = "1";//user.getUid();
        //判断用户是否对改课点过赞
        if (userLikeService.findCourseLikeRecordByCidAndUid(uid, cid) == null) {
            UserLike userLike = new UserLike().setUid(uid).setCid(cid);
            //新增一条点赞记录
            userLikeService.addLikeRecord(userLike);
            return new ResponseEntity(001, "点赞成功");
        }
        //删除点赞记录
        userLikeService.delLikeRecordByCidAndUid(uid, cid);
        return new ResponseEntity<>(002, "取消点赞");
    }
    @ApiOperation("查看某个课程的点赞记录")
    @GetMapping("findLikeRecord/{cid}")
    public ResponseEntity findLikeRecord(@ApiParam("课程id")@PathVariable String cid){
        return new ResponseEntity(003,"查看某个课程的点赞人数",userLikeService.courseLikeCountByCid(cid));
    }
}
